﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuoiThoProject.DAL;

namespace TuoiThoProject.BLL
{
    public class GmailBLL
    {
        NewsDataContext data = new NewsDataContext();
        public List<tblPicasa> getGmail_All()
        {
            return (from g in data.tblPicasas
                    select g).ToList();
        }

        public List<tblPicasa> getGmail_isDefault(bool isDefault)
        {
            return (from g in data.tblPicasas
                    where g.IsDefault == isDefault
                    select g).ToList();
        }

        public void AddAccount(string GmailAccount, string Password, bool isDefault)
        {
            tblPicasa p = new tblPicasa();
            p.cGmailId = GmailAccount;
            p.cPassword = Password;
            p.IsDefault = isDefault;

            if (isDefault == true)
            {
                var query = from p1 in data.tblPicasas
                            where p1.IsDefault == true
                            select p1;

                foreach (tblPicasa item in query)
                {
                    item.IsDefault = false;
                }
            }

            data.tblPicasas.InsertOnSubmit(p);
            data.SubmitChanges();
        }

        public void UpdateAccount(int Id, string GmailAccount, string Password, bool isDefault)
        {
            tblPicasa p = data.tblPicasas.SingleOrDefault(p1 => p1.nId == Id);
            p.cGmailId = GmailAccount;
            p.cPassword = Password;
            p.IsDefault = isDefault;

            if (isDefault == true)
            {
                var query = from p1 in data.tblPicasas
                            where p1.IsDefault == true
                            select p1;

                foreach (tblPicasa item in query)
                {
                    item.IsDefault = false;
                }
            }
            data.SubmitChanges();
        }

        public void DeleteAccount(int Id)
        {
            tblPicasa p = data.tblPicasas.SingleOrDefault(p1 => p1.nId == Id);
            data.tblPicasas.DeleteOnSubmit(p);
            data.SubmitChanges();
        }
    }
}